package m.ai.lab;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 增强型数学计算
 * 
 * @author liuhao
 *
 */
public class Maths {
	public static class Index<R> {
		public final R ref;
		public final int index;

		Index(int index, R ref) {
			this.index = index;
			this.ref = ref;
		}
	}

	/**
	 * 实现ArgMax算法
	 * 
	 * @param comparator
	 * @param values
	 * @return
	 */
	public static <T> Index<T> argmax(Comparator<T> comparator, T[] values) {
		List<Index<T>> list = new ArrayList<>();
		for (int i = 0; i < values.length; i++) {
			list.add(new Index<T>(i, values[i]));
		}

		return list.stream().max((a, b) -> comparator.compare(a.ref, b.ref)).get();
	}

	public static final Comparator<Float> FLOAT_COMPARATOR = (a, b) -> (int) (a * 1000) - (int) (b * 1000);
}
